home *** CD-ROM | disk | FTP | other *** search
- /*
- ** $RCSfile: ShowSys.c,v $
- ** $Filename: ShowSys.c $
- ** $Revision: 0.8 $
- ** $Date: 1995/11/04 14:14:05 $
- **
- ** sysmon.library Task display command ShowSys (version 0.9)
- **
- ** (C) Copyright 1995 by Etienne Vogt
- */
-
- #include <exec/alerts.h>
- #include <exec/memory.h>
- #include <exec/execbase.h>
- #include <dos/dosextens.h>
- #include <dos/var.h>
- #include <dos/datetime.h>
- #include <workbench/startup.h>
- #include <devices/timer.h>
- #define __USE_SYSBASE
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <proto/utility.h>
- #include <proto/timer.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include "sysmon.h"
- #include "sysmon_protos.h"
- #include "sysmon_pragmas.h"
-
- #define TIBUFMAX 30
- #define TNAMEMAX 31
- #define NT_CLI NT_USER
-
- struct tiBuffer
- { struct TaskInfo tib_tinfo;
- char tib_name[TNAMEMAX];
- UBYTE tib_type;
- LONG tib_clinum;
- ULONG tib_stacksize;
- ULONG tib_sigalloc;
- ULONG tib_sigwait;
- ULONG tib_sigrecvd;
- ULONG tib_sigexcept;
- BYTE tib_pri;
- UBYTE tib_flags;
- UBYTE tib_state;
- };
-
- struct tiBufArray
- { struct tiBufArray *tba_Link;
- struct tiBuffer tba_bufs[TIBUFMAX];
- };
-
- struct SysmonBase *SysmonBase;
- struct Library *TimerBase;
- struct timerequest myTimeReq;
- static struct RDArgs *myrda;
-
- int main(void);
- static void cleanexit(ULONG rc);
- static char *uptimestr(struct EClockVal *clockval, ULONG clockrate);
- static char *cputimestr(struct EClockVal *clockval, ULONG clockrate);
- static char *taskstate(UBYTE state);
- static char *taskflags(UBYTE flags);
- static char *tasktype(UBYTE type, LONG clinum);
- static char *bstr2c(BSTR bstring);
- static char *cpustr(UWORD cpuflags);
- static char *filterctrl(char *string);
- APTR SPrintf(STRPTR buffer, STRPTR format, ...);
- void KPrintf(STRPTR format, ...);
- void freetibufs(struct tiBufArray *tibufs);
-
- static UBYTE version[] = "$VER: ShowSys 0.9 (4.11.95)";
- static UBYTE template[] = "FULL/S";
-
- #define OPT_FULL 0
- #define OPTMAX 1
-
- int main(void)
- {
- ULONG EClockRate;
- struct tiBufArray *tibufs;
- LONG opts[OPTMAX];
-
- SysmonBase = NULL;
- TimerBase = NULL;
- myrda = NULL;
-
- if ((SysmonBase = (struct SysmonBase *)OpenLibrary("sysmon.library",0)) == NULL)
- { if (DOSBase->dl_lib.lib_Version >= 36) Printf("ShowSys : Couldn't open sysmon.library.\n");
- cleanexit(20);
- }
-
- if (OpenDevice(TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&myTimeReq, 0L))
- { Alert(AT_Recovery|AG_OpenDev|AO_TimerDev);
- cleanexit(100);
- }
- else
- { char KickStart[8], NodeName[16];
- struct EClockVal UpTime;
- struct DateTime SysTime;
- UBYTE strDate[LEN_DATSTRING], strTime[LEN_DATSTRING];
-
- memset((char *)opts, 0, sizeof(opts));
- if ((myrda = ReadArgs(template, opts, NULL)) == NULL)
- { PrintFault(IoErr(),"ShowSys");
- cleanexit(20);
- }
-
- TimerBase = (struct Library *)myTimeReq.tr_node.io_Device;
- EClockRate = ReadEClock(&UpTime);
- if (GetVar("KickStart", KickStart, sizeof(KickStart) - 1, GVF_GLOBAL_ONLY) > 0)
- Printf("AmigaOS %s", KickStart);
- else Printf("AmigaOS ?????");
- if (GetVar("HostName", NodeName, sizeof(NodeName) - 1, GVF_GLOBAL_ONLY) > 0)
- Printf(" at %s", NodeName);
- else Printf(" ");
- DateStamp(&SysTime.dat_Stamp);
- SysTime.dat_Format = FORMAT_DOS;
- SysTime.dat_Flags = 0;
- SysTime.dat_StrDay = NULL;
- SysTime.dat_StrDate = strDate;
- SysTime.dat_StrTime = strTime;
- if (DateToStr(&SysTime)) Printf(" %s %s", SysTime.dat_StrDate, SysTime.dat_StrTime);
- else Printf(" ");
- Printf(" UpTime :%s\n", uptimestr(&UpTime, EClockRate));
- }
-
- Printf("Address Name State Pri CPU [%.5s] Type\n",cpustr(SysBase->AttnFlags));
- if (opts[OPT_FULL]) Printf("> Stack SigAlloc SigWait SigRecvd SigExcpt Flags Dispatch\n");
-
- if ((tibufs = AllocVec(sizeof(struct tiBufArray), MEMF_PUBLIC | MEMF_CLEAR)) == NULL)
- { Printf("ShowSys : No memory for TaskInfo buffers.\n");
- cleanexit(20);
- }
- else
- { struct TaskInfo *tinfo = NULL;
- struct tiBufArray *tba = tibufs;
- struct tiBuffer *buf;
- int j, i = 0, numtask = 0;
-
- Forbid();
- while (tinfo = smNextTaskInfo(tinfo))
- { struct Task *task;
- struct CommandLineInterface *cli;
- char *comname;
- int n;
-
- numtask++;
- buf = &tba->tba_bufs[i];
- memcpy(buf, tinfo, sizeof(struct TaskInfo));
- task = tinfo->ti_Task;
- if (task->tc_Node.ln_Name) strcpy(buf->tib_name, filterctrl(task->tc_Node.ln_Name));
- else strcpy(buf->tib_name, "<< No Name >>");
- buf->tib_flags = task->tc_Flags;
- buf->tib_state = task->tc_State;
- buf->tib_sigalloc = task->tc_SigAlloc;
- buf->tib_sigwait = task->tc_SigWait;
- buf->tib_sigrecvd = task->tc_SigRecvd;
- buf->tib_sigexcept = task->tc_SigExcept;
- if (buf->tib_state == TS_FROZENW) buf->tib_state = TS_FROZEN;
- if (buf->tib_state == TS_WAIT && task->tc_SigWait == 0) buf->tib_state = TS_STOP;
- buf->tib_pri = task->tc_Node.ln_Pri;
- buf->tib_type = task->tc_Node.ln_Type;
- if (buf->tib_type == NT_PROCESS && (cli = BADDR(((struct Process *)task)->pr_CLI)))
- { buf->tib_clinum = ((struct Process *)task)->pr_TaskNum;
- buf->tib_type = NT_CLI;
- buf->tib_stacksize = cli->cli_DefaultStack * sizeof(ULONG);
- if (cli->cli_Module && (n = strlen(buf->tib_name)) < TNAMEMAX - 4)
- { comname = filterctrl(FilePart(bstr2c(cli->cli_CommandName)));
- strcat(buf->tib_name, " (");
- strncat(buf->tib_name, comname, TNAMEMAX - n - 4);
- strcat(buf->tib_name, ")");
- }
- }
- else if (buf->tib_type == NT_PROCESS) buf->tib_stacksize = ((struct Process *)task)->pr_StackSize;
- else buf->tib_stacksize = (ULONG)task->tc_SPUpper - (ULONG)task->tc_SPLower;
-
- if (++i == TIBUFMAX)
- { struct tiBufArray *tba1;
-
- if (tba1 = AllocVec(sizeof(struct tiBufArray), MEMF_PUBLIC | MEMF_CLEAR))
- { tba->tba_Link = tba1;
- tba = tba1;
- i = 0;
- }
- else
- { Permit();
- freetibufs(tibufs);
- Printf("ShowSys : No memory for TaskInfo buffers.\n");
- cleanexit(20);
- }
- }
- }
- Permit();
-
- for (j = 0, tba = tibufs, i = 0 ; j < numtask ; j++)
- { buf = &tba->tba_bufs[i];
- Printf("%08lx %-30.30s %-5s %4ld %15s %-8s\n", buf->tib_tinfo.ti_Task,
- buf->tib_name, taskstate(buf->tib_state), buf->tib_pri,
- cputimestr(&buf->tib_tinfo.ti_CPUTime, EClockRate),
- tasktype(buf->tib_type, buf->tib_clinum));
- if (opts[OPT_FULL])
- { if (buf->tib_state == TS_WAIT || buf->tib_state == TS_FROZENW)
- Printf(">%7lu %08lx %08lx %08lx %08lx %8s %10lu\n", buf->tib_stacksize,
- buf->tib_sigalloc, buf->tib_sigwait, buf->tib_sigrecvd, buf->tib_sigexcept,
- taskflags(buf->tib_flags), buf->tib_tinfo.ti_DispCount);
- else
- Printf(">%7lu %08lx -------- %08lx %08lx %8s %10lu\n", buf->tib_stacksize,
- buf->tib_sigalloc, buf->tib_sigrecvd, buf->tib_sigexcept, taskflags(buf->tib_flags),
- buf->tib_tinfo.ti_DispCount);
- }
- if (CheckSignal(SIGBREAKF_CTRL_C))
- { Printf("*** BREAK ***\n");
- break;
- }
- if (++i == TIBUFMAX)
- { tba = tba->tba_Link;
- i = 0;
- }
- }
- freetibufs(tibufs);
- }
- cleanexit(0);
- }
-
- static void cleanexit(ULONG rc)
- {
- if (TimerBase) CloseDevice((struct IORequest *)&myTimeReq);
- if (SysmonBase) CloseLibrary((struct Library *)SysmonBase);
- if (myrda) FreeArgs(myrda);
- exit(rc);
- }
-
- static char *uptimestr(struct EClockVal *clockval, ULONG clockrate)
- { static char buffer[16];
- ULONG seconds;
- UWORD updays, uphours, upminutes, upseconds;
-
- seconds = (ULONG)((clockval->ev_hi * 4294967296.0 + clockval->ev_lo) / (double)clockrate);
- updays = seconds / (24*60*60);
- seconds %= 24*60*60;
- uphours = seconds / (60*60);
- seconds %= 60*60;
- upminutes = seconds / 60;
- upseconds = seconds % 60;
- SPrintf(buffer, "%4lu %02lu:%02lu:%02lu", updays, uphours, upminutes, upseconds);
- return buffer;
- }
-
- static char *cputimestr(struct EClockVal *clockval, ULONG clockrate)
- { static char buffer[18];
- ULONG millis;
- UWORD cpudays, cpuhours, cpuminutes, cpuseconds, cpumillis;
-
- millis = (ULONG)((clockval->ev_hi * 4294967296.0 + clockval->ev_lo) / (double)(clockrate / 1000));
- cpudays = millis / (24*60*60*1000);
- millis %= 24*60*60*1000;
- cpuhours = millis / (60*60*1000);
- millis %= 60*60*1000;
- cpuminutes = millis / (60*1000);
- millis %= 60*1000;
- cpuseconds = millis / 1000;
- cpumillis = millis % 1000;
- SPrintf(buffer, "%2lu %02lu:%02lu:%02lu.%03lu", cpudays, cpuhours, cpuminutes, cpuseconds, cpumillis);
- return buffer;
- }
-
- static char *taskflags(UBYTE flags)
- { static char buffer[9];
-
- buffer[0] = (flags & TF_LAUNCH) ? 'l' : '-';
- buffer[1] = (flags & TF_SWITCH) ? 's' : '-';
- buffer[2] = (flags & TF_EXCEPT) ? 'e' : '-';
- buffer[3] = (flags & TF_STACKCHK) ? 'c' : '-';
- buffer[4] = (flags & TF_ETASK) ? 'x' : '-';
- buffer[5] = (flags & (1L<<2)) ? '2' : '-';
- buffer[6] = (flags & (1L<<1)) ? '1' : '-';
- buffer[7] = (flags & TF_PROCTIME) ? 'p' : '-';
- buffer[8] = '\0';
- return buffer;
- }
-
- static char *taskstate(UBYTE state)
- {
- switch(state)
- { case TS_INVALID:
- return "INVLD";
- case TS_ADDED:
- return "ADDED";
- case TS_RUN:
- return "RUN";
- case TS_READY:
- return "READY";
- case TS_WAIT:
- return "WAIT";
- case TS_EXCEPT:
- return "EXCPT";
- case TS_REMOVED:
- return "REMVD";
- case TS_STOP:
- return "STOP";
- case TS_FROZEN:
- return "FROZN";
- default:
- return "?????";
- }
- }
-
- static char *tasktype(UBYTE type, LONG clinum)
- { static char buffer[10];
-
- switch(type)
- { case NT_TASK:
- SPrintf(buffer,"Task");
- break;
- case NT_PROCESS:
- SPrintf(buffer,"Process");
- break;
- case NT_CLI:
- SPrintf(buffer,"Cli %-4ld",clinum);
- break;
- default:
- SPrintf(buffer,"???");
- break;
- }
- return buffer;
- }
-
- #define AFB_68060 7
- #define AFF_68060 (1L<<7)
-
- static char *cpustr(UWORD cpuflags)
- {
- if (cpuflags & AFF_68060) return "68060";
- else if (cpuflags & AFF_68040) return "68040";
- else if (cpuflags & AFF_68030) return "68030";
- else if (cpuflags & AFF_68020) return "68020";
- else if (cpuflags & AFF_68010) return "68010";
- else return "68000";
- }
-
- static char *bstr2c(BSTR bstring)
- { static char buffer[256];
- char *ptr;
- int n;
-
- if ((ptr = BADDR(bstring)) == NULL || (n = *ptr++) == 0) return NULL;
- strncpy(buffer, ptr, n + 1);
- return buffer;
- }
-
- static char *filterctrl(char *string)
- { static char buffer[TNAMEMAX];
- int i;
-
- for (i = 0 ; *string && i < TNAMEMAX - 1 ; i++, string++)
- { if ((*string & 0x7f) < ' ') buffer[i] = '.';
- else buffer[i] = *string;
- }
- buffer[i] = 0;
- return buffer;
- }
-
- APTR SPrintf(STRPTR buffer, STRPTR format, ...)
- { return smVSPrintf(buffer, format, &format + 1);
- }
-
- void KPrintf(STRPTR format, ...)
- { smVKPrintf(format, &format + 1);
- }
-
- void freetibufs(struct tiBufArray *tibufs)
- { struct tiBufArray *nexttba;
-
- do
- { nexttba = tibufs->tba_Link;
- FreeVec(tibufs);
- } while (tibufs = nexttba);
- }
-